package cn.cmeizu.spring6.resource;

import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author cmeizu
 * @date 2023/2/19 13:58
 * 访问类路径下的资源
 */
public class ClassPathResourceDemo {
  public static void main(String[] args) {
    loadClassPathResource("cmeizu.txt");
  }

  public static void loadClassPathResource(String path) {
    //创建ClassPathResource对象
    ClassPathResource resource = new ClassPathResource(path);

    System.out.println("resource.getFilename() = " + resource.getFilename());
    System.out.println("resource.getDescription() = " + resource.getDescription());
    try {
      InputStream in = resource.getInputStream();
      byte[] b = new byte[1024];
      while (in.read(b) != -1) {
        System.out.println("new String(b) = " + new String(b).trim());
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

  }
}
